'Declare these fields in the Activity Globals section.
Private editableTable As UIEditableTable
Private tableActions As UIBtnFloating
Private columnNameDialog As UIImputDialog
Private xui As XUI

'Run this setup from Activity_Create.
editableTable.Initialize(Me, "EditableTable")
editableTable.AddToParent(Activity, 12dip, 12dip, 100%x - 24dip, 100%y - 24dip)
editableTable.SetLanguage("Auto") ' Follows the device language; use a code such as "es" or "en" to override.

editableTable.AddColumns(Array As String("Code", "Name", "Amount"))
editableTable.AddRow(Array As String("A-01", "First item", "36.39"))
editableTable.AddRow(Array As String("A-02", "Second item", "18.50"))
' Rows and columns can also be added later with AddEmptyRow / AddColumn from code.
editableTable.AddColumn("Status")
editableTable.AddEmptyRow
editableTable.SetTheme("Ocean") ' Also available: "Transparent" to show the parent background through the table.
editableTable.SetColumnType(2, editableTable.COLUMN_TYPE_DECIMAL) ' Use a decimal keypad for the Amount column.
editableTable.AddSearchBox ' Opens the floating search panel; column-menu search is scoped to that column.

'Optional: set an icon next to a header and an image plus a button in cells.
'Add status.png to the app's Files tab, or replace it with your own bitmap.
Dim statusImage As Bitmap = LoadBitmap(File.DirAssets, "status.png")
editableTable.SetHeaderImage(1, statusImage, 20dip, 20dip)
editableTable.SetCellImage(0, 0, statusImage, 22dip, 22dip)
Dim rowAction As UIButton
rowAction.Initialize(Me, "RowAction")
Dim actionCell As B4XView = editableTable.GetCellContentPanel(2, 0)
rowAction.AddToParent(actionCell, 3dip, 3dip, 80dip, 24dip, rowAction.STYLE_OUTLINED, 12, "Open", Colors.White, 0xFF168F86, 5)

'Floating action button with a built-in menu for adding rows and columns.
tableActions.Initialize(Me, "TableActions")
tableActions.AddToParent(Activity, 100%x - 70dip, 100%y - 82dip, 56dip, 56dip, 0xFF35B998)
tableActions.InitializeMenu(False, Activity) ' False opens the actions above the button.
tableActions.additemIcon("Add row", Colors.White, 14, Chr(0xE145), xui.CreateMaterialIcons(20), Colors.White, 0xFF35B998)
tableActions.additemIcon("Add column", Colors.White, 14, Chr(0xE8EC), xui.CreateMaterialIcons(20), Colors.White, 0xFF3478C8)

' Formulas support arithmetic (+, -, *, /), parentheses, A1 references, and SUM/SUMA, AVERAGE/PROMEDIO,
' MIN, MAX, COUNT/CONTAR and ABS. SUM and the other aggregate functions accept A1-style ranges.
' D1 multiplies the two Amount cells; D2 uses a saved custom function.
editableTable.SetColumnType(3, editableTable.COLUMN_TYPE_DECIMAL)
editableTable.SetFormula(3, 0, "=C1*C2")
Log("Calculated D1 value: " & editableTable.GetValue(3, 0)) ' 673.215
' Register a reusable function. Custom function and parameter names use letters only.
Dim doubleParameters As List = Array As String("x")
editableTable.SetFormulaFunction("DUPLICAR", doubleParameters, "x*2")
editableTable.SetFormula(3, 1, "=DUPLICAR(C1)")
' Standard functions can aggregate a range of cells.
editableTable.SetFormula(3, 2, "=SUMA(C1:C2)")
'Select a cell, then drag the small square at its lower-right corner to fill a rectangular range with that cell\'s displayed value.
' Function definitions are included by GetProperties / SaveProperties and restored by SetProperties / LoadProperties.
' editableTable.RemoveFormulaFunction("DUPLICAR")
' For interactive editing, tap fx, enter the expression, tap referenced cells, then press Enter to commit.

' Persist and restore the selected theme/colors with the app's settings file.
' Use File.DirInternal for app-private storage on Android.
editableTable.SaveProperties(File.DirInternal, "orders-table.properties")
' Later, after Initialize: editableTable.LoadProperties(File.DirInternal, "orders-table.properties")

' Call either delete method from your own row action:
' editableTable.DeleteSelectedRow
' editableTable.DeleteRow(0) ' Zero-based row index.

Private Sub EditableTable_CellClick(Col As Int, Row As Int)
	Log("Selected " & Col & ", " & Row & ": " & editableTable.GetValue(Col, Row))
End Sub

Private Sub EditableTable_ThemeChanged(ThemeName As String)
	Log("Active table theme: " & ThemeName)
End Sub

Private Sub RowAction_Click
	Log("Cell button clicked")
End Sub

Private Sub TableActions_ItemClicked(Item As String, Index As Int)
	Select Index
		Case 0
			editableTable.AddEmptyRow
		Case 1
			columnNameDialog.Initialize(Activity, Me, "NewColumn")
			columnNameDialog.setTitle("Add column")
			columnNameDialog.setIputTitle("Column name")
			columnNameDialog.setHintText("Enter a name")
			columnNameDialog.setSubmitText("Add")
			columnNameDialog.setCancelText("Cancel")
			columnNameDialog.show
	End Select
End Sub

Private Sub NewColumn_Submit(Text As String)
	Dim columnName As String = Text.Trim
	If columnName <> "" Then editableTable.AddColumn(columnName)
End Sub
